package com.ketangpai.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ketangpai.admin.dao.CourseDao;
import com.ketangpai.admin.entity.Course;
import com.ketangpai.admin.entity.UserCourse;
import com.ketangpai.admin.entity.dto.CourseDTO;
import com.ketangpai.admin.entity.param.CourseParam;
import com.ketangpai.admin.service.CourseService;
import com.ketangpai.admin.service.UserCourseService;
import com.ketangpai.admin.utils.AliOSSUtil;


import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 课程表(Course)表服务实现类
 *
 * @author makejava
 * @since 2023-06-14 01:00:26
 */
@Service("courseService")
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements CourseService {
    @Resource
    private CourseDao courseDao;
    @Resource
    private AliOSSUtil aliOSSUtil;
    @Resource
    private UserCourseService userCourseService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    public CourseDTO queryById(Integer id) {
        Course course = courseDao.selectById(id);
        CourseDTO courseDTO = new CourseDTO();
        BeanUtils.copyProperties(course, courseDTO);
        courseDTO.setStudent_number(userCourseService.getStuNumByCid(id));
        return courseDTO;
    }

    /**
     * 通过课程号查询单条数据
     */
    @Override
    public Course queryByCode(String code) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("course_code", code);
        return courseDao.selectOne(wrapper);
    }

    /**
     * 分页查询
     */
    public Map<String, Object> selectList(CourseParam param) {
        Page<Course> page = new Page<>(param.getPage(), param.getPageSize());
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (param.getName() != null) {
            wrapper.like("name", param.getName());
        }
        courseDao.selectPage(page, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return map;
    }

    /**
     * 新增数据
     */
    @Override
    public boolean insert(Course course) {
        String code = randomString();
        course.setCourseCode(code);
        if (course.getFile() != null) {//如果有文件
            String url = aliOSSUtil.upload(course.getFile());
            course.setBackgroundImage(url);
        }
        boolean b=courseDao.insert(course) > 0;
        addUserCourse(course);//添加创建人的用户课程
        return b;
    }

    /**
     * 添加创建人的用户课程
     */
    public void addUserCourse(Course course) {
        UserCourse userCourse = new UserCourse();
        userCourse.setCourseId(course.getId());
        userCourse.setUserId(course.getUserId());
        userCourse.setType(1);
        userCourseService.insert(userCourse);
    }

    public String randomString() {
        // 生成随机[a-z,A-Z]字符串
        String f = RandomStringUtils.randomAlphabetic(1).toUpperCase();
        // 生成指定长度的字母和数字组成的随机组合字符串
        String code = RandomStringUtils.randomAlphanumeric(5);
        while (queryByCode(code) != null) {
            code = RandomStringUtils.randomAlphanumeric(5);
        }
        return f + code;
    }

    /**
     * 修改数据
     */
    @Override
    public boolean update(Course course) {
        if (course.getFile() != null) {
            String url = aliOSSUtil.upload(course.getFile());
            course.setBackgroundImage(url);
        }
        return this.courseDao.updateById(course) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.courseDao.deleteById(id) > 0;
    }

}
